package com.capri.activity;


import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.Toast;

import com.capri.Constant.Constants;
import com.capri.R;
import com.capri.app.NurseHomeApp;
import com.capri.base.BaseActivity;
import com.capri.easypermission.EasyPermission;
import com.capri.easypermission.Permission;
import com.capri.model.INurse;
import com.capri.model.impl.NurseModel;
import com.capri.service.GeoService;
import com.capri.utils.PreferenceUtils;
import com.capri.utils.SystemUtil;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.orhanobut.logger.Logger;

import androidx.annotation.NonNull;

import es.dmoral.toasty.Toasty;


public class SplashActivity extends BaseActivity {

    private Handler delayHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(TextUtils.isEmpty(PreferenceUtils.GetUserToken())){
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                NurseHomeApp.getInstance().finishActivity(SplashActivity.this);
                SplashActivity.this.finish();
            }
            else{
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                NurseHomeApp.getInstance().finishActivity(SplashActivity.this);
                SplashActivity.this.finish();
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void setupView() {
        PermissionsUtil.requestPermission(getApplication(), new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permissions) {
                Intent intentService = new Intent(SplashActivity.this, GeoService.class);
                startService(intentService);
                delayHandler.sendMessageDelayed(Message.obtain(), 1000);
                TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                @SuppressLint("MissingPermission") String DEVICE_ID = tm.getDeviceId();
                Logger.i("DeviceId-->"+DEVICE_ID);
//                Constants.DEVICE_ID=DEVICE_ID;
                Constants.DEVICE_MODEL= SystemUtil.getSystemModel();
            }

            @Override
            public void permissionDenied(@NonNull String[] permissions) {
                Toasty.error(SplashActivity.this, "未能获得地理位置权限", Toast.LENGTH_SHORT, true).show();
            }
        }, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.READ_PHONE_STATE});
    }

    @Override
    public void setupData() {
    }

    @Override
    protected void onDestroy() {
        if (delayHandler != null) {
            delayHandler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }
}
